home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / JustDoIt / INIT / justdoit.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.1 KB  |  318 lines

  1. // Source Code generated by PatchMaker 1.1
  2. // a sick idea by Paul Baxter
  3. //
  4. // paul@paulbaxter.com
  5.  
  6. #if TARGET_CPU_68K && !TARGET_RT_MAC_CFM
  7.     #error Sorry Can't do that
  8. #endif
  9.  
  10. #include <MixedMode.h>
  11. #include <Appearance.h>
  12. #include <SpeechSynthesis.h>
  13. #include <A4Stuff.h>
  14. #include <stdio.h>
  15.  
  16.  
  17. // enums for routinedescriptor
  18. enum {
  19.     uppAlertProcInfo_0_0 = kPascalStackBased
  20.         |    RESULT_SIZE(kTwoByteCode)
  21.         |    STACK_ROUTINE_PARAMETER(1,kTwoByteCode)
  22.         |    STACK_ROUTINE_PARAMETER(2,kFourByteCode),
  23.     uppAlertProcInfo_1_0 = kPascalStackBased
  24.         |    RESULT_SIZE(kTwoByteCode)
  25.         |    STACK_ROUTINE_PARAMETER(1,kTwoByteCode)
  26.         |    STACK_ROUTINE_PARAMETER(2,kFourByteCode),
  27.     uppNMInstallProcInfo = kRegisterBased
  28.         |    REGISTER_RESULT_LOCATION(kRegisterD0)
  29.         |    RESULT_SIZE(kTwoByteCode)
  30.         |    REGISTER_ROUTINE_PARAMETER(1,kRegisterD1,kTwoByteCode)
  31.         |    REGISTER_ROUTINE_PARAMETER(2,kRegisterA0,kFourByteCode),
  32.     uppPatchExtraInfo    // not used
  33. };
  34.  
  35. // Function Prototypes
  36. extern pascal short myAlert_0_0Patch(short param1, long param2);
  37. extern pascal short myAlert_1_0Patch(short param1, long param2);
  38. extern pascal short myAlertPatch(short param1, long param2);
  39. extern pascal short myNMInstallPatch(short param1, long param2);
  40.  
  41. extern void LoadStrings(void);
  42. extern void InstallGestalt(void);
  43.  
  44. // Globals to hold original routine descriptors
  45. RoutineDescriptorPtr gOriginalAlert_0_0 = nil;
  46. RoutineDescriptorPtr gOriginalAlert_1_0 = nil;
  47. RoutineDescriptorPtr gOriginalNMInstall = nil;
  48.  
  49. SInt16    gJustDoItActivated    = false;
  50.  
  51.  
  52. #define kJustDoItGestalt            'Nike'
  53.  
  54. #define kMagicAreYouSureAlertID        2212
  55. #define kFinderSig                    'MACS'
  56.  
  57. #define kStringList                    128
  58. enum {
  59.     eBringFindertoFront                = 1,
  60.     eAllocationErr                    = 2
  61. };
  62.  
  63. Str255 gPleaseBringFindertoFront, gErrString;
  64.  
  65. void BringApppSigtoFront(ResType sig);
  66.  
  67. // Tell MetroWerks the procInfo for main
  68. ProcInfoType __procinfo =  kPascalStackBased;
  69. pascal void main(void)
  70. {
  71.     RoutineDescriptorPtr originaldesc, newdesc, unImplimentedTrapAddr;
  72.     UInt32 size;
  73.     UInt16 index;
  74.     THz theZone;
  75.  
  76.     // get globals
  77.     EnterCodeResource();
  78.     // detach ourselves
  79.     DetachResource(Get1Resource('INIT', 0));
  80.  
  81.     // make sure we are in the system heap
  82.     theZone = GetZone();
  83.     SetZone(SystemZone());
  84.  
  85.     // Load strings MUST HAPPEN BERFORE TRAP IS PATCHED
  86.     LoadStrings();
  87.  
  88.     InstallGestalt();
  89.  
  90.     unImplimentedTrapAddr = NGetTrapAddress(_Unimplemented, (_Unimplemented & 0x0800) ? ToolTrap : OSTrap);
  91.  
  92.     originaldesc = NGetTrapAddress(_Alert, (_Alert & 0x0800) ? ToolTrap : OSTrap);
  93.     if (originaldesc != unImplimentedTrapAddr) {
  94.         if (originaldesc->goMixedModeTrap != _MixedModeMagic) {
  95.             // Trap is 68K
  96.             size = sizeof(RoutineDescriptor) + (sizeof(RoutineRecord) * 1);
  97.             // allocate new descriptor
  98.             newdesc = (RoutineDescriptorPtr) NewPtrSys(size);
  99.             if (!newdesc) {
  100.                 DebugStr(gErrString);
  101.                 return;
  102.             }
  103.             // set fields of routine decscriptor
  104.             newdesc->goMixedModeTrap = _MixedModeMagic;
  105.             newdesc->version = 7;
  106.             newdesc->routineDescriptorFlags = 0;
  107.             newdesc->reserved1 = 0;
  108.             newdesc->reserved2 = 0;
  109.             newdesc->selectorInfo = 0;
  110.             newdesc->routineCount = 1;
  111.             index = 0;
  112.             gOriginalAlert_0_0 = originaldesc;
  113.             newdesc->routineRecords[index].procInfo = uppAlertProcInfo_0_0;
  114.             newdesc->routineRecords[index].reserved1 = 0;
  115.             newdesc->routineRecords[index].ISA = GetCurrentArchitecture();
  116.             newdesc->routineRecords[index].routineFlags = 0;
  117.             newdesc->routineRecords[index].procDescriptor = (ProcPtr)myAlert_0_0Patch;
  118.             newdesc->routineRecords[index].reserved2 = 0;
  119.             newdesc->routineRecords[index].selector = 0;
  120.             index = 1;
  121.             gOriginalAlert_1_0 = originaldesc;
  122.             newdesc->routineRecords[index].procInfo = uppAlertProcInfo_1_0;
  123.             newdesc->routineRecords[index].reserved1 = 0;
  124.             newdesc->routineRecords[index].ISA = GetCurrentArchitecture();
  125.             newdesc->routineRecords[index].routineFlags = 0;
  126.             newdesc->routineRecords[index].procDescriptor = (ProcPtr)myAlert_1_0Patch;
  127.             newdesc->routineRecords[index].reserved2 = 0;
  128.             newdesc->routineRecords[index].selector = 0;
  129.         }
  130.         else {
  131.             // Trap is PPC
  132.             size = sizeof(RoutineDescriptor) + (sizeof(RoutineRecord) * originaldesc->routineCount);
  133.             newdesc = (RoutineDescriptorPtr) NewPtrSys(size);
  134.             if (!newdesc) {
  135.                 DebugStr(gErrString);
  136.                 return;
  137.             }
  138.             BlockMoveData(originaldesc, newdesc, size);
  139.             for (index = 0; index <= newdesc->routineCount; index++) {
  140.                 if ((originaldesc->routineRecords[index].selector == 0) &&
  141.                 (originaldesc->routineRecords[index].ISA == 0) && (originaldesc->routineRecords[index].procInfo == uppAlertProcInfo_0_0)) {
  142.                     gOriginalAlert_0_0= NewRoutineDescriptor(originaldesc->routineRecords[index].procDescriptor,
  143.                                 uppAlertProcInfo_0_0, originaldesc->routineRecords[index].ISA);
  144.                     newdesc->routineRecords[index].procDescriptor = (ProcPtr)myAlert_0_0Patch;
  145.                     newdesc->routineRecords[index].ISA = GetCurrentArchitecture();
  146.                     continue;
  147.                 }
  148.                 if ((originaldesc->routineRecords[index].selector == 0) &&
  149.                 (originaldesc->routineRecords[index].ISA == 1) && (originaldesc->routineRecords[index].procInfo == uppAlertProcInfo_1_0)) {
  150.                     gOriginalAlert_1_0= NewRoutineDescriptor(originaldesc->routineRecords[index].procDescriptor,
  151.                                 uppAlertProcInfo_1_0, originaldesc->routineRecords[index].ISA);
  152.                     newdesc->routineRecords[index].procDescriptor = (ProcPtr)myAlert_1_0Patch;
  153.                     newdesc->routineRecords[index].ISA = GetCurrentArchitecture();
  154.                     continue;
  155.                 }
  156.             }
  157.         }
  158.         NSetTrapAddress((UniversalProcPtr) newdesc, _Alert, (_Alert & 0x0800) ? ToolTrap : OSTrap);
  159.     }
  160.  
  161.     originaldesc = NGetTrapAddress(_NMInstall, (_NMInstall & 0x0800) ? ToolTrap : OSTrap);
  162.     if (originaldesc != unImplimentedTrapAddr) {
  163.         if (originaldesc->goMixedModeTrap != _MixedModeMagic) {
  164.             // Trap is 68K
  165.             size = sizeof(RoutineDescriptor) + (sizeof(RoutineRecord) * 0);
  166.             // allocate new descriptor
  167.             newdesc = (RoutineDescriptorPtr) NewPtrSys(size);
  168.             if (!newdesc) {
  169.                 DebugStr(gErrString);
  170.                 return;
  171.             }
  172.             // set fields of routine decscriptor
  173.             newdesc->goMixedModeTrap = _MixedModeMagic;
  174.             newdesc->version = 7;
  175.             newdesc->routineDescriptorFlags = 0;
  176.             newdesc->reserved1 = 0;
  177.             newdesc->reserved2 = 0;
  178.             newdesc->selectorInfo = 0;
  179.             newdesc->routineCount = 0;
  180.             index = 0;
  181.             gOriginalNMInstall = originaldesc;
  182.             newdesc->routineRecords[index].procInfo = uppNMInstallProcInfo;
  183.             newdesc->routineRecords[index].reserved1 = 0;
  184.             newdesc->routineRecords[index].ISA = GetCurrentArchitecture();
  185.             newdesc->routineRecords[index].routineFlags = 4;
  186.             newdesc->routineRecords[index].procDescriptor = (ProcPtr)myNMInstallPatch;
  187.             newdesc->routineRecords[index].reserved2 = 0;
  188.             newdesc->routineRecords[index].selector = 0;
  189.         }
  190.         else {
  191.             // Trap is PPC
  192.             size = sizeof(RoutineDescriptor) + (sizeof(RoutineRecord) * originaldesc->routineCount);
  193.             newdesc = (RoutineDescriptorPtr) NewPtrSys(size);
  194.             if (!newdesc) {
  195.                 DebugStr(gErrString);
  196.                 return;
  197.             }
  198.             BlockMoveData(originaldesc, newdesc, size);
  199.             gOriginalNMInstall = originaldesc;
  200.             index = 0;
  201.             newdesc->routineRecords[index].procDescriptor = (ProcPtr)myNMInstallPatch;
  202.             newdesc->routineRecords[index].ISA = GetCurrentArchitecture();
  203.         }
  204.         NSetTrapAddress((UniversalProcPtr) newdesc, _NMInstall, (_NMInstall & 0x0800) ? ToolTrap : OSTrap);
  205.     }
  206.  
  207.     // restore to original heap
  208.     SetZone(theZone);
  209.     // restore globals
  210.     ExitCodeResource();
  211. }
  212.  
  213. void InstallGestalt(void)
  214. {
  215.     OSErr err = NewGestaltValue(kJustDoItGestalt, (long) &gJustDoItActivated);
  216. }
  217.  
  218. pascal short myAlert_0_0Patch(short param1, long param2)
  219. {
  220.     short result;
  221.     // Get globals
  222.     EnterCodeResource();
  223.  
  224.     result = myAlertPatch( param1, param2);
  225.  
  226.     // Release globals
  227.     ExitCodeResource();
  228.     return result;
  229. }
  230.  
  231. pascal short myAlert_1_0Patch(short param1, long param2)
  232. {
  233.     short result;
  234.     // Get globals
  235.     EnterCodeResource();
  236.  
  237.     result = myAlertPatch( param1, param2);
  238.  
  239.     // Release globals
  240.     ExitCodeResource();
  241.     return result;
  242. }
  243.  
  244. pascal short myAlertPatch(short param1, long param2)
  245. {
  246.     short result;
  247.     // Get globals
  248.     EnterCodeResource();
  249.  
  250.     if     ((gJustDoItActivated == false) || (param1 != kMagicAreYouSureAlertID)) {
  251.         result = CALL_TWO_PARAMETER_UPP( gOriginalAlert_1_0, uppAlertProcInfo_1_0, param1, param2);
  252.     }
  253.     else {
  254.         result = ok;
  255.     }
  256.  
  257.     // Release globals
  258.     ExitCodeResource();
  259.     return result;
  260. }
  261.  
  262. pascal short myNMInstallPatch(short param1, long param2)
  263. {
  264.     short result;
  265.     // Get globals
  266.     EnterCodeResource();
  267.  
  268.  
  269.     result = CallOSTrapUniversalProc(gOriginalNMInstall, uppNMInstallProcInfo, param1, param2);
  270.     if (gJustDoItActivated != false) {
  271.         NMRecPtr theNMRec = (NMRecPtr)param2;
  272.         ProcessSerialNumber PSN;
  273.  
  274.         if (theNMRec->nmStr && EqualString(theNMRec->nmStr, gPleaseBringFindertoFront, false, false)) {
  275.             BringApppSigtoFront(kFinderSig);
  276.         }
  277.     }
  278.  
  279.     // Release globals
  280.     ExitCodeResource();
  281.     return result;
  282. }
  283.  
  284. void BringApppSigtoFront(ResType sig)
  285. {
  286.     ProcessSerialNumber PSN = {0, kNoProcess};
  287.     ProcessInfoRec    info = {
  288.             sizeof(ProcessInfoRec), 
  289.             0,
  290.             0,
  291.             0,
  292.             0,
  293.             0,
  294.             0,
  295.             0,
  296.             0,
  297.             0,
  298.             0,
  299.             0};
  300.  
  301.     
  302.     while (!GetNextProcess(&PSN)) {
  303.         if (!GetProcessInformation(&PSN,&info)) {
  304.             if (sig == info.processSignature) {
  305.                 SetFrontProcess(&PSN);
  306.                 return;
  307.             }
  308.         }
  309.     }
  310. }
  311.  
  312.  
  313. void LoadStrings(void)
  314. {
  315.     GetIndString(gPleaseBringFindertoFront, kStringList, eBringFindertoFront);
  316.     GetIndString(gErrString, kStringList, eAllocationErr);
  317. }
  318.